import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams ,ViewController,LoadingController,ToastController} from 'ionic-angular';

import {UserService} from "../../providers/UserService";
import {UserInfo} from "../../model/UserInfo";

import {StorageService} from "../../providers/StorageService";
import {STATUS_SUCCESS, USERFACE_PATH} from "../../providers/Constants";

import {HeadfacePage} from "../headface/headface";
import {TabsPage} from "../tabs/tabs";
import {BaseUI} from "../../common/baseui";

@IonicPage()
@Component({
  selector: 'page-user-info',
  templateUrl: 'user-info.html',
})
export class UserInfoPage extends BaseUI{

  errorMessage:any;
  userFaceImg:String;
  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public userService :UserService,
              public userInfo :UserInfo,
              public  viewCtrl : ViewController,
              public  storageService:StorageService,
              public loadingCtrl :LoadingController,
              public toastCtrl:ToastController) {
    super();
  }


  ionViewDidEnter() {
    this.loadUserInfoPage();
  }

  loadUserInfoPage(){
    let loader = this.loadingCtrl.create({content: "正在获取数据...",});
    loader.present();
    // super.showLoading(this.loadingCtrl,"正在获取数据...");
    var id =this.storageService.read<UserInfo>('UserInfo').id ;
    this.userService.getUserInfo(id).subscribe(
      res=>{
        if(res["status"]==0){
          this.userInfo=res["data"];
          this.storageService.write('UserInfo', res["data"]);//更新本地
          this.userFaceImg = USERFACE_PATH+"/"+this.userInfo.userFace;
          console.log(this.userInfo);
          loader.dismiss();
        }else {
          this.viewCtrl.dismiss();
        }

      }, error => {
        this.errorMessage=error;
        alert(this.errorMessage);
        loader.dismiss();
        super.showToast(this.toastCtrl, "网络请求失败", 'middle');

      });
  };
  //注销
  logout() {
    this.storageService.remove('UserInfo');
    this.viewCtrl.dismiss();
  }
  gotoHeadface(){
    this.navCtrl.push(HeadfacePage);
    parent.onload;
  }

  //更新信息
  updateInfo(){
    let loader = this.loadingCtrl.create({
      content: "正在提交...",
    });

    this.userService.updatInfo(this.userInfo).then(res => {
      if(res["status"]==STATUS_SUCCESS){
        this.toastCtrl.create({message:res["msg"], duration: 4500}).present();
        loader.dismiss();
        this.storageService.write('UserInfo', res["data"]);
        this.viewCtrl.dismiss();
        return;
      }
      loader.dismiss();
      this.toastCtrl.create({message:res["msg"], duration: 4500}).present();

    });
  }

}
